package Gui;
import javax.swing.*;
import javax.swing.SpringLayout;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Toolkit;
public class EditorWindow implements ActionListener {
    
    private final ImageIcon TEST_ICON = new ImageIcon("icons/page.png","test icon");
    private final String PROBLEM_ICON = "icons/chart_organisation.png";
    private final String QUESTION_ICON = "icons/comment.png";
    private final String FREE_ANSWER_ICON = "icons/pencil.png";
    private final String TEST_ANSWER_ICON = "icons/tick.png";
     
    JMenuItem menuFileNewTest;
    JMenuItem menuFileOpenTest;
    JMenuItem menuFileClose;
    
    JMenuBar menuBar;
    JMenu menuFile;
    
    JTree testTree;
    
    JFrame editorWindow;
    Container editorWindowContainer;
    
    JPanel editingPanel;
    JPanel testEditingPanel;
    JPanel problemEditingPanel;
    JPanel questionEditingPanel;
    JPanel freeAnswerEditingPanel;
    JPanel testAnswerEditingPanel;
    
    public EditorWindow() {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  
        //menu bar items
        menuFileNewTest = new JMenuItem("create new test");
        menuFileOpenTest = new JMenuItem("open existing test");
        menuFileClose = new JMenuItem("close editor");
        menuFileClose.addActionListener(this);
        menuFileClose.setActionCommand("close application");
        
        //menu bar
        menuBar = new JMenuBar();
        menuFile = new JMenu("File");
        
        menuFile.add(menuFileNewTest);
        menuFile.add(menuFileOpenTest);
        menuFile.add(menuFileClose);
        
        menuBar.add(menuFile);
        
        ExtendedTreeNode defaultTest = new ExtendedTreeNode("Test",ExtendedTreeNode.TEST_CATEGORY);
        ExtendedTreeNode defaultProblem = new ExtendedTreeNode("Problem",ExtendedTreeNode.PROBLEM_CATEGORY);
        ExtendedTreeNode defaultQuestion = new ExtendedTreeNode("Question",ExtendedTreeNode.QUESTION_CATEGORY);
        ExtendedTreeNode defaultAnswer = new ExtendedTreeNode("Free answer",ExtendedTreeNode.FREE_ANSWER_CATEGORY);
        
        defaultQuestion.insert(defaultAnswer,0);
        defaultProblem.insert(defaultQuestion,0);
        defaultTest.insert(defaultProblem,0);
        
        DefaultTreeModel defaultModel = new DefaultTreeModel(defaultTest);
        
        testTree = new JTree(defaultModel);
          
        /*
        DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
        renderer.setOpenIcon(null);
        renderer.setClosedIcon(null);
        renderer.setLeafIcon(null);
        testTree.setCellRenderer(renderer);
         */
        
        testTree.addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent e) {
                ExtendedTreeNode selectedNode = (ExtendedTreeNode)e.getSource();
                // TEST ICON is selected
                if(selectedNode.getNodeCategory()==ExtendedTreeNode.TEST_CATEGORY) {
                    //creating TEST EDITING PANEL
                        testEditingPanel = new JPanel(new SpringLayout());
                        
                        String[] labels = {"Test name: ","Author: ","Introduction text: ","Hold time: "};
                        
                        for(int i=0; i<labels.length; i++) {
                            JLabel l = new JLabel(labels[i], JLabel.TRAILING);
                            testEditingPanel.add(l);
                            JTextField textField = new JTextField(10);
                            l.setLabelFor(textField);
                            testEditingPanel.add(textField);
                        }
                        
                        SpringUtilities.makeCompactGrid(testEditingPanel,labels.length,2,6,6,6,6);
                        
                        //adding TEST EDITING PANEL to TEST OVERALL PANEL to avoid oversizing
                        //and to WINDOW CONTAINER to its place and revalidating the container
                        JPanel testEditingOverallPanel = new JPanel();
                        testEditingOverallPanel.add(testEditingPanel);
                        editorWindowContainer.add(testEditingOverallPanel,"Center");
                        editorWindowContainer.validate();
                    }
                
                    // PROBLEM ICON is selected
                    if(selectedNode.getNodeCategory()==ExtendedTreeNode.PROBLEM_CATEGORY) {
                        problemEditingPanel = new JPanel();
                        problemEditingPanel.add(new JLabel("Problem editing"));
                        editorWindowContainer.add(problemEditingPanel,"Center");
                        editorWindowContainer.validate();
                    }
                
                    // QUESTION ICON is selected
                    if(selectedNode.getNodeCategory()==ExtendedTreeNode.QUESTION_CATEGORY) {
                        problemEditingPanel = new JPanel();
                        problemEditingPanel.add(new JLabel("Question editing"));
                        editorWindowContainer.add(problemEditingPanel,"Center");
                        editorWindowContainer.validate();
                    }            
            }
        });
        
        JScrollPane scrollingTestTree = new JScrollPane(testTree);
        scrollingTestTree.setSize(350,500);
        
        editorWindow = new JFrame("test editor");
        editorWindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        editorWindow.setSize(700,500);
        
        editingPanel = new JPanel();
        
        editorWindowContainer = editorWindow.getContentPane();
        editorWindowContainer.setLayout(new BorderLayout());
        editorWindowContainer.add(scrollingTestTree,"West");
        editorWindowContainer.add(editingPanel,"Center");
        
        editorWindow.setJMenuBar(menuBar);
        
        editorWindow.setVisible(true);
        editorWindow.setLocation((screenSize.width-700)/2,(screenSize.height-500)/2);
    }
    
    public void actionPerformed(ActionEvent ae) {
        if(ae.getActionCommand().equals("close application")) {
            System.exit(0);
        }
    }
    
    private String testTitle(String title) {
        return "<html><img src="+TEST_ICON+"> "+title+"</html>";
    }
    
    private String problemTitle(String title) {
        return "<html><img src="+PROBLEM_ICON+"> "+title+"</html>";
    }
    
    private String questionTitle(String title) {
        return "<html><img src="+QUESTION_ICON+"> "+title+"</html>";
    }
    
    private String freeAnswerTitle(String title) {
        return "<html><img src="+FREE_ANSWER_ICON+"> "+title+"</html>";
    }
    
    private String testAnswerTitle(String title) {
        return "<html><img src="+TEST_ANSWER_ICON+"> "+title+"</html>";
    }
}